package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyh on 2017/3/17.
 */

public abstract class MyBaseAdapter<T> extends BaseAdapter {

    protected List<T> mList;

    protected Context context;

    protected LayoutInflater layoutInflater;

    public MyBaseAdapter(Context context) {
        this.context = context;
        mList = new ArrayList<>();
        layoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<T> list) {
        if (this.mList != null) {
            this.mList.clear();
            this.mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addData(List<T> list) {
        if (this.mList != null) {
            this.mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public T getItem(int position) {
        return mList != null && mList.size() > 0 ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void clear() {
        if (mList != null) {
            mList.clear();
            mList = null;
        }
        this.context = null;
    }
}
